// 生成模拟命令数据
const generateCommands = (count) => {
  const commands = []
  const types = ['shell', 'sql', 'ansible', 'python', 'powershell']
  const platforms = ['linux', 'windows', 'all']
  const statuses = ['enabled', 'disabled', 'deprecated']
  
  for (let i = 1; i <= count; i++) {
    const type = types[Math.floor(Math.random() * types.length)]
    const platform = platforms[Math.floor(Math.random() * platforms.length)]
    
    let content = ''
    switch (type) {
      case 'shell':
        content = `#!/bin/bash\necho "执行命令 ${i}"\nls -la /tmp\ndf -h`
        break
      case 'sql':
        content = `SELECT * FROM users WHERE status = 'active' LIMIT 10;`
        break
      case 'ansible':
        content = `- name: 更新系统\n  apt:\n    update_cache: yes\n    upgrade: yes`
        break
      case 'python':
        content = `import os\nimport sys\n\nprint("系统信息:")\nprint(os.uname())`
        break
      case 'powershell':
        content = `Get-Process | Where-Object {$_.CPU -gt 10} | Sort-Object CPU -Descending`
        break
    }
    
    commands.push({
      id: i,
      name: `命令-${i.toString().padStart(3, '0')}`,
      description: `这是一个${platform}平台的${type}类型命令，用于执行系统操作。`,
      type,
      platform,
      content,
      status: statuses[Math.floor(Math.random() * statuses.length)],
      creator: `user${Math.floor(Math.random() * 10) + 1}`,
      createTime: new Date(Date.now() - Math.floor(Math.random() * 365) * 86400000).toISOString(),
      updateTime: new Date(Date.now() - Math.floor(Math.random() * 30) * 86400000).toISOString(),
      execCount: Math.floor(Math.random() * 1000)
    })
  }
  
  return commands
}

// 命令相关的模拟数据
export function setupCommandMock(mock) {
  // 模拟的命令数据
  const commands = generateCommands(100)
  
  // 获取命令列表
  mock.onGet('/api/command/list').reply((config) => {
    const { page = 1, pageSize = 10, keyword = '', type = '', platform = '' } = config.params || {}
    
    let filteredCommands = [...commands]
    
    // 按类型过滤
    if (type) {
      filteredCommands = filteredCommands.filter(
        command => command.type === type
      )
    }
    
    // 按平台过滤
    if (platform) {
      filteredCommands = filteredCommands.filter(
        command => command.platform === platform || command.platform === 'all'
      )
    }
    
    // 关键词过滤
    if (keyword) {
      filteredCommands = filteredCommands.filter(
        command => command.name.includes(keyword) || 
                  command.description.includes(keyword) || 
                  command.content.includes(keyword)
      )
    }
    
    // 分页
    const total = filteredCommands.length
    const start = (page - 1) * pageSize
    const end = start + Number(pageSize)
    const pageData = filteredCommands.slice(start, end)
    
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: {
          list: pageData,
          pagination: {
            page: Number(page),
            pageSize: Number(pageSize),
            total
          }
        }
      }
    ]
  })
  
  // 获取命令类型列表
  mock.onGet('/api/command/types').reply(() => {
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: ['shell', 'sql', 'ansible', 'python', 'powershell']
      }
    ]
  })
  
  // 获取命令详情
  mock.onGet(/\/api\/command\/detail\/\d+/).reply((config) => {
    const id = parseInt(config.url.split('/').pop())
    const command = commands.find(c => c.id === id)
    
    if (command) {
      return [
        200,
        {
          code: 200,
          message: 'success',
          data: command
        }
      ]
    } else {
      return [
        200,
        {
          code: 404,
          message: '命令不存在',
          data: null
        }
      ]
    }
  })
} 